package cn.iocoder.yudao.module.member.vip.controller.app;

import cn.iocoder.yudao.framework.business.order.pojo.vo.OrderSubmitVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.idempotent.core.annotation.Idempotent;
import cn.iocoder.yudao.framework.idempotent.core.keyresolver.impl.UserIdempotentKeyResolver;
import cn.iocoder.yudao.module.member.vip.dto.AppVipOrderSubmitDTO;
import cn.iocoder.yudao.module.member.vip.service.AppVipOrderService;
import cn.iocoder.yudao.module.member.vip.vo.AppVipOrderVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 会员卡订单")
@RestController
@RequestMapping("/member/vip-order")
@Validated
public class AppVipOrderController {
    @Resource
    private AppVipOrderService appVipOrderService;

    @Operation(summary = "分页")
    @GetMapping("/page")
    public CommonResult<PageResult<AppVipOrderVO>> getPage(@Valid PageParam param) {
        return success(appVipOrderService.getPage(param.getPageNo(), param.getPageSize(), getLoginUserId()));
    }

    @Idempotent(timeout = 5, message = "重复下单，请稍候再试", keyResolver = UserIdempotentKeyResolver.class)
    @PostMapping("/create")
    @Operation(summary = "创建订单", description = "购买会员卡，返回交易单号")
    public CommonResult<OrderSubmitVO> submit(@Valid @RequestBody AppVipOrderSubmitDTO reqVO) {
        OrderSubmitVO respVO = appVipOrderService.submit(reqVO, getLoginUserId());
        return success(respVO);
    }

}